package com.qn.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/26 11:03
 */
@Configuration
public class RedisConfig {

    @Bean(name="redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        //由于GenericJackson2JsonRedisSerializer这个序列化器对待LocalDateTime类型字段进行序列化的时候给LocaDatetime对象增加了很多的字段，
        //导致反序列会失败。 解决方案：
//        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}
